package com.zh.work.controller.platform.virtual;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zh.common.entity.JSONResult;

import com.zh.work.dao.platform.virtual.VirtualRateTypeMapper;
import com.zh.work.entity.virtual.VirtualRateEntity;
import com.zh.work.entity.virtual.VirtualRateTypeEntity;
import com.zh.work.service.platform.virtual.VirtualRateTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description: 费率实体类管理
 * @Author: Li.HuiNeng
 * @Create in: 2019/09/11
 * @Version: 1.0.1
 */
@Api(value = "费率实体类管理控制器", produces = "application/json", tags = {"费率实体类管理控制器"})
@RestController
@RequestMapping("/virtualRateType")
public class VirtualRateTypeController {

    /**
     * @Description: 简易crud时使用
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2019/09/11
     */
    @Autowired
    private VirtualRateTypeMapper mapper;

    /**
     * @Description: 有业务逻辑需要处理时使用
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2019/09/11
     */
    @Autowired
    private VirtualRateTypeService service;

    /**
     * @Description: redis实例
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2019/09/11
     */
    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation(value = "新增一条记录", tags = {"putAddVirtualRateType"}, notes = "新增一条记录")
    @PutMapping("/putAddVirtualRateType")
    @ResponseBody
    public JSONResult<Object> putAddVirtualRateType(@ApiParam(value = "费率实体类", name = "VirtualRateTypeEntity")
                                                   @RequestBody VirtualRateTypeEntity virtualRateTypeEntity) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        int i = service.putAddRateType(virtualRateTypeEntity);
        try {
            if (i > 0) {
                jsonResult.setCode(200);
                jsonResult.setMessage("添加完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setSuccess(false);
                if (i == -1) jsonResult.setMessage("添加费率信息失败!");
                else if (i == -2) jsonResult.setMessage("添加费率类型失败!");
                return jsonResult;
            }
        } catch (Exception e) {
            jsonResult.setCode(e.hashCode());
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            e.printStackTrace();
        }
        return jsonResult;
    }


    @ApiOperation(value = "通过id修改信息", tags = {"putModVirtualRateTypeById"}, notes = "通过id修改信息")
    @PutMapping("/putModVirtualRateTypeById")
    @ResponseBody
    public JSONResult<Object> putModVirtualRateTypeById(@ApiParam(value = "费率实体类", name = "virtualRateTypeEntity")
                                                          @RequestBody VirtualRateTypeEntity virtualRateTypeEntity) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        int i = service.putModVirtualRateTypeById(virtualRateTypeEntity);
        try {
            if (i > 0) {
                jsonResult.setCode(200);
                jsonResult.setMessage("修改完成");
                jsonResult.setSuccess(true);
            } else {
                if (i == -1) jsonResult.setMessage("修改费率信息失败!");
                else if (i == -2) jsonResult.setMessage("修改费率类别失败!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setCode(e.hashCode());
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "根据主键删除记录", tags = {"deleteVirtualRateTypeById"}, notes = "根据主键删除记录")
    @DeleteMapping("/deleteVirtualRateTypeById")
    public JSONResult<Object> deleteVirtualRateTypeById(@ApiParam(value = "费率实体类", name = "VirtualRateTypeEntity") VirtualRateTypeEntity virtualRateTypeEntity) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            if (virtualRateTypeEntity.deleteById()) {
                jsonResult.setCode(200);
                jsonResult.setMessage("删除完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("删除失败,请检查参数是否填写完整!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "根据主键列表批量删除记录", tags = {"deleteVirtualRateTypeBatchByIds"}, notes = "该传参时 请选择raw(application/json)格式传入")
    @DeleteMapping("/deleteVirtualRateTypeBatchByIds")
    @ResponseBody
    public JSONResult<Object> deleteVirtualRateTypeBatchByIds(@ApiParam(value = "主键集合", name = "ids")@RequestParam("ids") @RequestBody List<Integer> ids) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            int c = this.mapper.deleteBatchIds(ids);
            if (c > 0) {
                jsonResult.setCode(200);
                jsonResult.setMessage("删除完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("删除失败,请检查参数是否填写完整!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "根据id查询详细信息", tags = {"getVirtualRateTypeById"}, notes = "根据id查询详细信息")
    @GetMapping("/getVirtualRateTypeById")
    public JSONResult<VirtualRateTypeEntity> getVirtualRateTypeById(@ApiParam(value = "费率实体类", name = "VirtualRateTypeEntity") VirtualRateTypeEntity virtualRateTypeEntity) {
        JSONResult<VirtualRateTypeEntity> jsonResult = new JSONResult<>();
        try {
            VirtualRateTypeEntity result = virtualRateTypeEntity.selectById(virtualRateTypeEntity.getId());
            if (result != null) {
                jsonResult.setCode(200);
                jsonResult.setMessage("查询完成");
                jsonResult.setSuccess(true);
                jsonResult.setData(result);
            } else {
                jsonResult.setMessage("未查询到相应信息，请检查参数是否正确");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }


    @ApiOperation(value = "分页查询参数数据集", tags = {"getPagesVirtualRateTypes"}, notes = "分页查询参数数据集")
    @GetMapping("/getPagesVirtualRateTypes")
    public JSONResult<List<VirtualRateTypeEntity>> getPagesVirtualRateTypes(
            @ApiParam(value = "起始页", name = "pageNo")Integer pageNo,
            @ApiParam(value = "当前页显示数量", name = "pageSize")Integer pageSize,
            @ApiParam(value = "费率实体类", name = "VirtualRateTypeEntity") VirtualRateTypeEntity virtualRateTypeEntity) {
        JSONResult<List<VirtualRateTypeEntity>> jsonResult = new JSONResult<>();
        try {
            IPage<VirtualRateTypeEntity> result = this.service.selectVirtualRateTypePages(new Page<>(pageNo, pageSize), virtualRateTypeEntity);
            jsonResult.setTotal(result.getTotal());
            jsonResult.setData(result.getRecords());
            jsonResult.setMessage("操作成功");
            jsonResult.setSuccess(true);
            jsonResult.setCode(200);
        } catch (Exception e) {
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "查询指定费率信息", tags = {"getVirtualRateInfo"}, notes = "查询指定费率信息")
    @GetMapping("/getVirtualRateInfo")
    public JSONResult<List<VirtualRateEntity>> getVirtualRateInfo(@RequestParam(value = "rateCode") String rateCode) {
        JSONResult<List<VirtualRateEntity>> jsonResult = new JSONResult<>();
        VirtualRateEntity virtualRateEntity = new VirtualRateEntity();
        virtualRateEntity.setRateCode(rateCode);
        List<VirtualRateEntity> list = service.getVirtualRateInfo(virtualRateEntity);
        try {
            jsonResult.setData(list);
            jsonResult.setMessage("操作成功");
            jsonResult.setSuccess(true);
            jsonResult.setCode(200);
        } catch (Exception e) {
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "查询费率类型列表", tags = {"getVirtualRateTypeList"}, notes = "查询费率类型列表")
    @GetMapping("/getVirtualRateTypeList")
    public JSONResult<List<VirtualRateTypeEntity>> getVirtualRateTypeList() {
        JSONResult<List<VirtualRateTypeEntity>> jsonResult = new JSONResult<>();
        List<VirtualRateTypeEntity> list = service.getVirtualRateTypeList();
        try {
            jsonResult.setData(list);
            jsonResult.setMessage("操作成功");
            jsonResult.setSuccess(true);
            jsonResult.setCode(200);
        } catch (Exception e) {
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }


}
